﻿//异步刷新，
//tab：信息回填的ContentPane
//callback：回调函数
function AjaxGet(control,action,param,text,tab)
{
    var url = '/'+GetBasePath()+'/'+control+'/'+action+'.castle';  
    url = url.replace('//','/');
    if(param != null && param != "")
    {
        url += '?'+ param;
    }
    dojo.xhrGet({
            url: url,
            handleAs: "text",
            encoding: "utf-8",
            preventCache: true,
            content: 
            {
			    text:text,
			    encoding: "utf-8"
		    },
            load: function(data)
            {
                if (data.indexOf("document.frmErrMsg.submit();")>-1)
                {
                    //TODO:将来有可能的话，也让这块跳转到公共的异常处理页，而不是简单的弹出提示                    
                    //window.location="/LISInfoAcquisition/Errors/ErrorRedirector.castle";
                    var str=data.substring(data.indexOf('[95DBECB2-5AED-42E0-8EFD-8ABC7575C327]'),data.lastIndexOf('[95DBECB2-5AED-42E0-8EFD-8ABC7575C327]')).replace("[95DBECB2-5AED-42E0-8EFD-8ABC7575C327]","")
                    HideLoading('divCovering');
                    Ext.MessageBox.alert('错误提示',str);
                    return;
                }
                AjaxPostCallback(data,tab);
            },
            error: function(data){}
       }); 
}

//异步刷新，
//tab：信息回填的ContentPane
//callback：回调函数
function AjaxPost(control,action,form,param,tab,callback)
{
    //document.getElementById(form).cation = action;
    var url = "/"+GetBasePath()+"/"+control+"/"+action+".castle";  
    url = url.replace('//','/');
    if(param != null && param != "")
    {
        url += "?"+ param;
    }
    dojo.xhrPost
    (
        {
            url: url,
            handleAs: "text",
            encoding: "utf-8",
            preventCache: true,
            content: 
            {
			    fileFields: "DocPath"
		    },
            load: function(data)
            {
                //判断是否要停在本页，并给出提示信息
                if (data.indexOf("B853C291-337C-4B68-BE6F-6F56112BBCCA")>-1)
                {
                    RunScripts(data.replace("B853C291-337C-4B68-BE6F-6F56112BBCCA",""))
                    HideLoading('divCovering');
                    return;
                }
                //是否发生被monorail捕获的异常;
                if (data.indexOf("document.frmErrMsg.submit();")>-1)
                {
                    //TODO:将来有可能的话，也让这块跳转到公共的异常处理页，而不是简单的弹出提示                    
                    //window.location="/LISInfoAcquisition/Errors/ErrorRedirector.castle";
                    var str=data.substring(data.indexOf('[95DBECB2-5AED-42E0-8EFD-8ABC7575C327]'),data.lastIndexOf('[95DBECB2-5AED-42E0-8EFD-8ABC7575C327]')).replace("[95DBECB2-5AED-42E0-8EFD-8ABC7575C327]","")
                    HideLoading('divCovering');
                    Ext.MessageBox.alert('错误提示',str);
                    return;
                }
                 AjaxPostCallback(data,tab);
                 if(callback != undefined)
                 {
                     eval(callback);
                 }
            },
            error: function(data)
            {
                HideLoading('divCovering');
                Ext.MessageBox.alert('错误提示',data.message);
            },
            timeout: 20000,
            form: form 
        }
    );
}

//AjaxPost的子函数，获取数据后执行的动作
function AjaxPostCallback(data,tab)
{
    try
    {
        if(tab != undefined && tab != null)
        {
            var DijitTab = dijit.byId(tab);
            if(DijitTab)
            {
                DijitTab.setContent(data);
            }
        }
        //如果直接运行RunScripts方法，则js语句会并发运行，而不是在加载页面结束后运行。
        DelayTimeFunction(RunScripts,0,data);
    }
    catch(e)
    {
       HideLoading('divCovering');
       Ext.MessageBox.alert('错误提示',e.message);
    }
}

//AjaxPostCallback的子函数，执行JS动作
function RunScripts(data)
{
    var searchStart = 0 ;
    while(true)
    {        
        //增加动态执行javascript语句的方法
        var startScriptIndex = startScriptIndex = data.indexOf('//startScript',searchStart) + 13;
        if(startScriptIndex >=16)
        {
            var endScriptIndex = endScriptIndex = data.indexOf('//endScript',startScriptIndex);
            
            if(startScriptIndex > 0 && endScriptIndex > startScriptIndex)
            {
                var scriptToRun = data.substr(startScriptIndex,endScriptIndex - startScriptIndex);
                
                if(scriptToRun.trim().length > 0)
                {
                    eval(scriptToRun);
                }
                
                searchStart = endScriptIndex + 15;
            }
            else
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
};